package com.lzc.config;

import com.lzc.interception.LoginInterception;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterception())
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/im/login/**",
                        "/login/**",
                        "/im/file/upload/head/img",
                        "/im/file/load/head/img/**",
                        "/im/file/load/img/**",
                        "/im/file/scan/article/**");
    }

    // 远程调用
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    // 跨域
    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration config = new CorsConfiguration();

        config.addAllowedOrigin("*");

        config.setAllowCredentials(false);

        config.addAllowedMethod("*");

        config.addAllowedHeader("*");
        config.addExposedHeader("*");

        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",config);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
